package com.seeyon.apps.kk.manager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.seeyon.apps.kk.manager.datasync.AbstractDataSyncManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.GlobalNames;
import com.seeyon.ctp.common.authenticate.domain.User;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.flag.BrowserEnum;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.portal.portlet.manager.DeskCollaborationProcessManager;

/**
 * 人事单据流程处理
 * 
 * @author Administrator
 *
 */
public class HRFlowProcessManager extends AbstractDataSyncManager {
	private static final Log log = LogFactory.getLog(HRFlowProcessManager.class);
	private FreemarkFlowManager freemarkFlowManager;

	public FreemarkFlowManager getFreemarkFlowManager() {
		return freemarkFlowManager;
	}

	public void setFreemarkFlowManager(FreemarkFlowManager freemarkFlowManager) {
		this.freemarkFlowManager = freemarkFlowManager;
	}

	/**
	 * GHR_单据流程自动跳之人事节点 休假单
	 * 
	 * @throws BusinessException
	 * @throws NumberFormatException
	 */
	public void autoXJProcess() throws NumberFormatException, BusinessException {
		String sql = this.freemarkFlowManager.fltTemplateIntoString("sql/ghr/xjd/FIND_ALL_FLOW_XJDSQL.ftl", null);
		String fid = "";
		for (int i = 0; i < 4; i++) {
			List<Map<String, Object>> result = this.jdbcTemplate.queryForList(sql);
			for (Map<String, Object> map : result) {
				String[] memberSubjects = MapUtils.getString(map, "members").split(",");
				String memberId = MapUtils.getString(map, "member_id");
				fid = MapUtils.getString(map, "fid");
				if (ArrayUtils.contains(memberSubjects, memberId)) {
					Map<String, String> res = new HashMap<String, String>();
					res.put("affairId", MapUtils.getString(map, "id"));
					res.put("memberid", memberId);// 当前处理人ID
					res.put("comment", "系统自动处理！");// 处理意见
					res.put("attitude", "1");// 态度：1同意，2不同意，0已阅
					finishaffair(res);
				}
			}
		}
		Map<String, Object> map = new HashMap<>();
		if(!"".equals(fid)){
			map.put("ID", fid);
			String updatesql = freemarkFlowManager.fltTemplateIntoString("sql/ghr/xjd/UPDATE_STAT_XJDSQL.ftl", map);
			this.jdbcTemplate.update(updatesql);
		}
	}

	/**
	 * GHR_单据流程自动跳之人事节点 加班单
	 * 
	 * @throws BusinessException
	 * @throws NumberFormatException
	 */
	public void autoJBProcess() throws NumberFormatException, BusinessException {
		String sql = this.freemarkFlowManager.fltTemplateIntoString("sql/ghr/jbd/FIND_ALL_FLOW_JBDSQL.ftl", null);
		String fid = "";
		for (int i = 0; i < 4; i++) {
			List<Map<String, Object>> result = this.jdbcTemplate.queryForList(sql);
			for (Map<String, Object> map : result) {
				String[] memberSubjects = MapUtils.getString(map, "members").split(",");
				String memberId = MapUtils.getString(map, "member_id");
				fid = MapUtils.getString(map, "fid");
				if (ArrayUtils.contains(memberSubjects, memberId)) {
					Map<String, String> res = new HashMap<String, String>();
					res.put("affairId", MapUtils.getString(map, "id"));
					res.put("memberid", memberId);// 当前处理人ID
					res.put("comment", "系统自动处理！");// 处理意见
					res.put("attitude", "1");// 态度：1同意，2不同意，0已阅
					finishaffair(res);
				}
			}
		}
		Map<String, Object> map = new HashMap<>();
		if(!"".equals(fid)){
			map.put("ID", fid);
			String updatesql = freemarkFlowManager.fltTemplateIntoString("sql/ghr/jbd/UPDATE_STAT_JBDSQL.ftl", map);
			this.jdbcTemplate.update(updatesql);
		}
	}

	/**
	 * GHR_单据流程自动跳之人事节点 出差单
	 * 
	 * @throws BusinessException
	 * @throws NumberFormatException
	 */
	public void autoCCProcess() throws NumberFormatException, BusinessException {
		String sql = this.freemarkFlowManager.fltTemplateIntoString("sql/ghr/ccd/FIND_ALL_FLOW_CCDSQL.ftl", null);
		String fid = "";
		for (int i = 0; i < 4; i++) {
			List<Map<String, Object>> result = this.jdbcTemplate.queryForList(sql);
			for (Map<String, Object> map : result) {
				String[] memberSubjects = MapUtils.getString(map, "members").split(",");
				String memberId = MapUtils.getString(map, "member_id");
				fid = MapUtils.getString(map, "fid");
				if (ArrayUtils.contains(memberSubjects, memberId)) {
					Map<String, String> res = new HashMap<String, String>();
					res.put("affairId", MapUtils.getString(map, "id"));
					res.put("memberid", memberId);// 当前处理人ID
					res.put("comment", "系统自动处理！");// 处理意见
					res.put("attitude", "1");// 态度：1同意，2不同意，0已阅
					finishaffair(res);
				}
			}
		}
		Map<String, Object> map = new HashMap<>();
		if(!"".equals(fid)){
			String updatesql = freemarkFlowManager.fltTemplateIntoString("sql/ghr/ccd/UPDATE_STAT_CCDSQL.ftl", map);
			this.jdbcTemplate.update(updatesql);
		}
	}

	/**
	 * GHR_单据流程自动跳之人事节点 考勤单
	 * 
	 * @throws BusinessException
	 * @throws NumberFormatException
	 */
	public void autoKQProcess() throws NumberFormatException, BusinessException {
		String sql = this.freemarkFlowManager.fltTemplateIntoString("sql/ghr/kqd/FIND_ALL_FLOW_KQDSQL.ftl", null);
		String fid = "";
		for (int i = 0; i < 4; i++) {
			List<Map<String, Object>> result = this.jdbcTemplate.queryForList(sql);
			for (Map<String, Object> map : result) {
				String[] memberSubjects = MapUtils.getString(map, "members").split(",");
				String memberId = MapUtils.getString(map, "member_id");
				fid = MapUtils.getString(map, "fid");
				if (ArrayUtils.contains(memberSubjects, memberId)) {
					Map<String, String> res = new HashMap<String, String>();
					res.put("affairId", MapUtils.getString(map, "id"));
					res.put("memberid", memberId);// 当前处理人ID
					res.put("comment", "系统自动处理！");// 处理意见
					res.put("attitude", "1");// 态度：1同意，2不同意，0已阅
					finishaffair(res);
				}
			}
		}
		Map<String, Object> map = new HashMap<>();
		if(!"".equals(fid)){
			String updatesql = freemarkFlowManager.fltTemplateIntoString("sql/ghr/kqd/UPDATE_STAT_KQDSQL.ftl", map);
			this.jdbcTemplate.update(updatesql);
		}
	}

	public void finishaffair(Map<String, String> param) throws NumberFormatException, BusinessException {
		OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
		User user = new User();
		V3xOrgMember sender = orgManager.getMemberById(Long.valueOf(param.get("memberid").toString()));
		user.setId(sender.getId());
		user.setLoginName(sender.getLoginName());
		user.setName(sender.getName());
		user.setAccountId(sender.getOrgAccountId());
		user.setBrowser(BrowserEnum.IE);
		user.setLoginAccount(sender.getOrgAccountId());
		AppContext.putThreadContext(GlobalNames.SESSION_CONTEXT_USERINFO_KEY, user);
		DeskCollaborationProcessManager deskCollaborationProcessManager = (DeskCollaborationProcessManager) AppContext
				.getBean("deskCollaborationProcessManagerImpl");
		deskCollaborationProcessManager.finishWorkitemQuick(param);
	}

	@Override
	public boolean beforeGrabData() {
		try {
			autoXJProcess();
			autoJBProcess();
			autoCCProcess();
			autoKQProcess();
		} catch (Exception e) {
			e.printStackTrace();
			log.info("GHR单据月底自动跳过错误："+e.getMessage());
		}
		return false;
	}

	@Override
	public boolean grabData() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean addData() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateData() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean deleteData() {
		// TODO Auto-generated method stub
		return false;
	}
}
